<template>
  <div id="contain">
    <div>
      <el-form
        :model="seachform"
        label-width="100px"
        class="seachform"
        style="width: 940px"
      >
        <el-form-item label="广告名称:" style="width: 250px">
          <el-input v-model="seachform.name"></el-input>
        </el-form-item>
        <el-form-item label="广告Id:" style="width: 250px">
          <el-input v-model="seachform.adId"></el-input>
        </el-form-item>
        <el-form-item label="发布者:" style="width: 220px">
          <el-input v-model="seachform.realName"></el-input>
        </el-form-item>
        <el-button type="primary" @click="search" class="searchbtn"
          >搜索</el-button
        >
      </el-form>
    </div> 
    <div>
      <el-table
        :data="tableList"
        style="width: 945px"
        stripe
        :row-style="rowStyle"
        :cell-style="cellStyle"
        height="730px"
      >
        <el-table-column prop="name" label="广告名" align="center">
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="发布时间"
          align="center"
        ></el-table-column>
        <el-table-column prop="status" label="状态" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="mini" @click="edit(scope.row)">查看</el-button>
            <el-button type="mini" style="color: red" @click="clear(scope.row)"
              >撤销</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="page"
        :page-sizes="[10, 15, 20]"
        :page-size="limit"
        layout="sizes, prev, pager, next"
        :total="totalCount"
      >
      </el-pagination>
    </div>
    <el-dialog title="查看广告信息" :visible.sync="dialogFormVisible">
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="广告名称" style="width: 500px">
          <el-input v-model="form.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="广告编号" style="width: 500px">
          <el-input v-model="form.adId" disabled></el-input>
        </el-form-item>
        <el-form-item label="广告图片">
          <img
            :src="form.pic"
            alt="图片加载失败"
            style="width: 380px; height: 220px"
          />
        </el-form-item>
        <el-form-item label="广告内容">
          <el-input
            type="textarea"
            :rows="8"
            v-model="form.content"
            disabled
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="this.dialogFormVisible = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      token: window.localStorage.getItem("token"),
      page: 1,
      limit: 10,
      totalCount: 0,
      tableList: [],
      dialogFormVisible: false,
      form: {},
      fileList: [],
      imgForm: new FormData(),
      imgUrl: "",
      fileName: "",
      fileUrl: "",
      seachform: {},
    };
  },
  mounted() {
    this.getlist();
  },
  methods: {
    getlist() {
      this.$axios({
        method: "post",
        url: "/ad/list",
        headers: { token: this.token },
        data: {
          page: this.page,
          limit: this.limit,
        },
      }).then((res) => {
        // console.log("广告列表");
        // console.log(res);
        if (res.data.code === 0) {
          this.totalCount = res.data.page.totalCount;
          this.tableList = res.data.page.regard;
          this.tableList.forEach((item) => {
            item.status =
              item.status === -1
                ? "审核未通过"
                : item.status === 0
                ? "未审核"
                : item.status === 1
                ? "审核通过"
                : "撤销";
          });
        }
        if (res.data.code === 500) {
          this.$message({
            type: "error",
            message: res.data.msg,
          });
        }
      });
    },
    rowStyle() {
      return { height: "60px" };
    },
    cellStyle(k) {
      if (k.columnIndex === 2) {
        if (k.row.status === "审核通过") {
          return { color: "#33CC00" };
        } else if (k.row.status === "未审核") {
          return { color: "blue" };
        } else {
          return { color: "red" };
        }
      }
    },
    handleSizeChange(val) {
      this.limit = val;
      this.getlist();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getlist();
    },
    edit(k) {
      this.form = k;
      this.dialogFormVisible = true;
    },
    clear(k) {
      let id = k.adId;
      this.$axios({
        method: "post",
        url: "/ad/del",
        headers: { token: this.token },
        data: { adId: id },
      }).then((res) => {
        // console.log(res);
        if (res.data.code === 0) {
          this.$message({
            type: "success",
            message: "撤销成功！",
          });
          this.getlist();
        }
      });
    },
    search() {
      this.seachform.page = this.page;
      this.seachform.limit = this.limit;
      let data = this.seachform;
      // console.log(data);
      this.$axios({
        method: "post",
        url: "/ad/list",
        headers: { token: this.token },
        data: data,
      }).then((res) => {
        // console.log(res);
        if (res.data.code === 0) {
          if (res.data.page) {
            this.totalCount = res.data.page.totalCount;
            this.tableList = res.data.page.regard;
            this.tableList.forEach((item) => {
              item.status =
                item.status === 1
                  ? "审核通过"
                  : item.status === 0
                  ? "未审核"
                  : "未通过或撤销";
            });
          } else {
            this.tableList = [];
          }
        }
        if (res.data.code === 500) {
          this.$message({
            type: "error",
            message: res.data.msg,
          });
        }
      });
    },
  },
};
</script>
<style  scoped>
.seachform {
  margin: 10px 0 0 0;
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
}
.searchbtn {
  width: 80px;
  height: 40px;
  margin: 0 25px;
}
</style>>